/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.jspx.txweb.vo;

import com.jspx.sober.annotation.Column;
import com.jspx.sober.annotation.Table;
import com.jspx.txweb.table.Member;
import com.jspx.txweb.table.MemberDept;
import jspx.user.table.PositionCertTerm;
import com.jspx.utils.MapUtil;
import com.jspx.utils.StringUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenyuan on 14-3-14.
 *
 */
@Table(name = "jspx_position_member_vo", caption = "人员持有证书视图",create = false)
public class PositionMemberVO extends MemberDept {

    @Column(caption = "组织机构ID", notNull = true)
    private long organizeId = 0;

    @Column(caption = "组织机构", length = 50)
    private String organize = StringUtil.empty;

    public void setOrganize(String organize) {
        this.organize = organize;
    }

    public String getOrganize() {
        return organize;
    }

    public long getOrganizeId() {
        return organizeId;
    }

    public void setOrganizeId(long organizeId) {
        this.organizeId = organizeId;
    }


    //0:不符满足  1:满足要求 统计证书是否齐全   department
    @Column(caption = "证书是否齐全")
    private int meetType = 1;
    public int getMeetType() {
        getLackCert();
        if (lackNum==0) meetType = 1;
        else meetType = -1;
        return meetType;
    }

    public void setMeetType(int meetType) {
        this.meetType = meetType;
    }

    /**

     [8:安检员职业资格证书 中级;58:危险品运输证]
     [30:安检员职业资格证书 高级;58:危险品运输证]
     [54:安检员职业资格证书 初级;58:危险品运输证]
     * @param term
     * @return
     */
    private Map<Long,String> checkCerTerm(String term)
    {
        String[] certArray = StringUtil.split(term,StringUtil.SEMICOLON);
        Map<Long,String> certIdMap = new HashMap<Long,String>();
        for (String certAnd:certArray)
        {
            String[] certList = StringUtil.split(certAnd,StringUtil.SEMICOLON);
            for (String cert:certList)
            {

                long certId = StringUtil.toLong(StringUtil.trim(StringUtil.replace(StringUtil.substringBefore(cert, ":"), "[", "")));
                if  (certId>0) certIdMap.put(certId,StringUtil.trim(StringUtil.replace(StringUtil.substringAfter(cert, ":"), "]", "")));
            }
        }

        Map<Long,String> haveCertMap  = new HashMap<Long, String>();
        haveCertMap.putAll(haveCert);
        for (long haveCertId:haveCertMap.keySet())
        {
            certIdMap.remove(haveCertId);
        }
        return certIdMap;
    }

    @Column(caption = "缺少的证书")
    private Map<Long,String> lackCert  = new HashMap<Long, String>();
    public String getLackCert() {
        if (lackCert.isEmpty())
        {
            //放入条件判断是否满足岗位要求 begin
            String[] cerTermArray = StringUtil.split(StringUtil.replace(positionCertTerm.getCertTerm(),StringUtil.CRLF,""),"]");
            for (String term:cerTermArray)
            {
                term = StringUtil.trim(term);
                if (StringUtil.isNULL(term)) continue;
                Map<Long,String> needCertMap =  checkCerTerm(term + "]");
                if (needCertMap.isEmpty())
                {
                    lackNum = 0;
                    lackCert.clear();
                    break;
                }  else
                {
                    for (Long certId:needCertMap.keySet())
                    {
                        if (!lackCert.containsKey(certId))
                            lackCert.put(certId,needCertMap.get(certId));
                    }
                    lackNum = needCertMap.size();
                }
            }
            //放入条件判断是否满足岗位要求 end

            //判断缺少哪些证书begin
            //上边已经判断了一次,但当有多个条件的时候会有重复出现,下边必须清理一次
            if (lackNum!=0)
            {
                //不满足条件的证书,减去已经有的证书,就是缺少的证书
                for (Long certId:haveCert.keySet())
                {
                    lackCert.remove(certId);
                }
            }
            //判断缺少哪些证书end
        }
        return MapUtil.toString(lackCert, StringUtil.SEMICOLON);
    }
    //缺少 的证书

    public void setLackCert(long certId,String cert) {
        if (certId>0) lackCert.put(certId,cert);
    }


    @Column(caption = "缺少证书数")
    private int lackNum  = 0;
    public int getLackNum() {
        return lackNum;
    }

    public void setLackNum(int lackNum) {
        this.lackNum = lackNum;
    }

    //多个,使用 , 分割开
    @Column(caption = "岗位证书条件")
    private PositionCertTerm positionCertTerm  = new PositionCertTerm();
    public PositionCertTerm getPositionCertTerm() {
        return positionCertTerm ;
    }
    public void setPositionCertTerm(PositionCertTerm positionCertTerm) {
        this.positionCertTerm  = positionCertTerm ;
    }

    //多个,使用 , 分割开
    @Column(caption = "拥有的证书")
    private Map<Long,String> haveCert  = new HashMap<Long, String>();
    public String getHaveCert() {
        return MapUtil.toString(haveCert,StringUtil.SEMICOLON);
    }

    public void setHaveCert(long certId,String cert) {
        if (certId>0) haveCert.put(certId,cert);
    }

    //多个,使用 , 分割开
    @Column(caption = "过期的证书")
    private Map<Long,String> overdueCert  = new HashMap<Long, String>();
    public String getOverdueCert() {
        return MapUtil.toString(overdueCert,StringUtil.SEMICOLON);
    }

    public void setOverdueCert(long certId,String cert) {
        if (certId>0)  overdueCert.put(certId,cert);
    }

    //多个,使用 , 分割开
    @Column(caption = "将过期证书")
    private Map<Long,String> warningCert  = new HashMap<Long, String>();
    public String getWarningCert() {
        return MapUtil.toString(warningCert,StringUtil.SEMICOLON);
    }

    public void setWarningCert(long certId,String cert)   {
        if (certId>0) warningCert.put(certId,cert);
    }


}